package cn.edu.scau.cmi.chenbingquan.client;

import cn.edu.scau.cmi.chenbingquan.multiton.Marshal;
import cn.edu.scau.cmi.chenbingquan.singleton.Chairman;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

// 综合性实验客户类
public class ComprehensiveExperimentClient extends Application {
	private Pane root = new Pane();		// 根面板
	private Rectangle personImage;		// 学生图片(图片填充矩形)
	private Text messageText;			// 学生信息
	private ListView<String> list = new ListView<>();		// 模式选择列表
	private Button sure;		// 确定按钮
	private int num = -1;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		root.setStyle("-fx-background-color: ALICEBLUE");
		root.setPrefSize(680, 455);
		
		personImage = new Rectangle(270, 330);
		personImage.setFill(new ImagePattern(new Image("cn/edu/scau/cmi/chenbingquan/image/陈炳权照片.jpg")));
		personImage.setArcWidth(15);
		personImage.setArcHeight(15);
	    personImage.setLayoutX(20);
	    personImage.setLayoutY(20);
		
		messageText = new Text("学号：201727020101" + "\n姓名：陈炳权");
		messageText.setFont(new Font(17));
		messageText.setFill(Color.ORANGE);
		messageText.setLayoutX(65);
		messageText.setLayoutY(400);
		
		list.getItems().addAll("实验一：接口编程", "实验二：单例模式", "实验二：多例模式", "实验三：工厂模式",
							   "实验三：简单工厂", "实验三：抽象工厂", "实验四：类适配器", "实验四：对象适配器",
							   "实验五：安全组合", "实验五：一致组合", "实验六：基于DI,ORM模式的老师指导学生毕业设计");
		list.setFixedCellSize(30);
		list.setPrefSize(310, 332);
		list.setLayoutX(350);
		list.setLayoutY(20);
		
		sure = new Button("确定");
		sure.setStyle("-fx-background-radius: 10");
		sure.setPrefSize(60, 35);
		sure.setLayoutX(475);
		sure.setLayoutY(385);
		root.getChildren().addAll(personImage, messageText, list, sure);
		
		Scene scene = new Scene(root);
		scene.getStylesheets().add(getClass().getResource("listView.css").toExternalForm());
		stage.setScene(scene);
		stage.getIcons().add(new Image("cn/edu/scau/cmi/chenbingquan/image/窗口左上角图标.png"));
		stage.setTitle("综合性实验");
		stage.setResizable(false);
		stage.show();
		
		addEvents();
	}
	
	// 添加事件
	private void addEvents() {
		// 确定按钮点击事件
		sure.setOnMouseClicked(action -> {
			String select = list.getSelectionModel().getSelectedItem();
			if(select == null) {
				showMessageStage("请先选择模式!!");
				
			} else if(select.equals("实验一：接口编程")) {
				InterProgrammingClient ipc = new InterProgrammingClient();
				ipc.start(new Stage());
					
			} else if(select.equals("实验二：单例模式")) {
				SingletonClient sc = new SingletonClient();
				Chairman.instance = null;
				sc.start(new Stage());
				
			} else if(select.equals("实验二：多例模式")) {
				MultitonClient mc = new MultitonClient();
				Marshal.instances.clear();
				mc.start(new Stage());
				
			} else if(select.equals("实验三：工厂模式")) {
				FactoryClient fc = new FactoryClient();
				fc.start(new Stage());
				
			} else if(select.equals("实验三：简单工厂")) {
				SimpleFactoryClient sfc = new SimpleFactoryClient();
				sfc.start(new Stage());
				
			} else if(select.equals("实验三：抽象工厂")) {
				AbstractFactoryClient afc = new AbstractFactoryClient();
				afc.start(new Stage());
				
			} else if(select.equals("实验四：类适配器")) {
				ClassAdapterClient cac = new ClassAdapterClient();
				cac.start(new Stage());
				
			} else if(select.equals("实验四：对象适配器")) {
				ObjectAdapterClient oac = new ObjectAdapterClient();
				oac.start(new Stage());
				
			} else if(select.equals("实验五：安全组合")) {
				SafeCompositeClient scc = new SafeCompositeClient();
				scc.start(new Stage());
				
			} else if(select.equals("实验五：一致组合")) {
				ConsistentCompositeClient ccc = new ConsistentCompositeClient();
				ccc.start(new Stage());
				
			} else if(select.equals("实验六：基于DI,ORM模式的老师指导学生毕业设计")) {
				SpringHibernateSessionClient shsc = new SpringHibernateSessionClient();
				shsc.start(new Stage());
			}
		});
		
		// 根面板背景颜色动态改变
		Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(2), e -> {
			num++;
			if(num%2 == 0) {
				root.setStyle("-fx-background-color: linear-gradient(to right, #1f4037, #99f2c8)");
			} else {
				root.setStyle("-fx-background-color: linear-gradient(to right, #c31432, #240b36)");
			}
		}));
		timeline.setCycleCount(Timeline.INDEFINITE);
		timeline.play();
	}
	
	// 显示信息窗口
	private void showMessageStage(String message) {
		VBox vBox = new VBox(60);
		vBox.setAlignment(Pos.CENTER);
		vBox.setStyle("-fx-background-color: ANTIQUEWHITE");
		vBox.setPrefSize(300, 250);
					
		Text text = new Text(message);
		text.setStyle("-fx-font-size: 25; -fx-fill: orange"); 
					
		Button button = new Button("确定");
		button.setPrefSize(60, 30);
		vBox.getChildren().addAll(text, button);
					
		Stage stage = new Stage();
		Scene scene = new Scene(vBox);
		stage.setScene(scene);
		stage.show();
					
		// 事件
		button.setOnMouseClicked(e -> stage.close() ); 		// 关闭窗口
	}
}
